package vue;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;

import lib.Ensemble;
import vue.composants.Boutton;

public class IntersectionDialog extends JDialog implements ActionListener{

	private FenetrePrincipale mere;
	private Boutton bouttonConfirmer;
	private JComboBox premierEnsembleCombo;
	private JComboBox deuxiemeEnsembleCombo;
	private JLabel text;
	
	public IntersectionDialog(FenetrePrincipale mere)
	{
		super(mere);
		//this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setBounds(500, 500, 500, 500);
		this.setMere(mere);
		
		
		
		this.bouttonConfirmer = new Boutton("Confirmer");
		this.bouttonConfirmer.addActionListener(this);		
		System.out.println(this.getMere().getMesEnsemble());
		if(!this.mere.getMesEnsemble().isEmpty())
		{
			this.getContentPane().setLayout(new GridLayout());
			
			this.premierEnsembleCombo = new JComboBox(this.getMere().getMesEnsemble().toArray());
			this.getContentPane().add(this.premierEnsembleCombo);
			
			this.text = new JLabel("Complementaire");
			this.getContentPane().add(this.text);
			
			this.deuxiemeEnsembleCombo = new JComboBox(this.getMere().getMesEnsemble().toArray());
			this.getContentPane().add(this.deuxiemeEnsembleCombo);
			
			this.getContentPane().add(this.bouttonConfirmer);
		}
		else
		{
			this.text = new JLabel("Aucun ensemble disponible");
			this.getContentPane().add(this.text);
		}
		
		this.pack();
		this.setVisible(true);
	}

	/**
	 * @return the mere
	 */
	public FenetrePrincipale getMere() {
		return mere;
	}

	/**
	 * @param mere the mere to set
	 */
	public void setMere(FenetrePrincipale mere) {
		this.mere = mere;
	}
	
	private void ajouteEnsemble()
	{
		Ensemble e1 = (Ensemble)this.premierEnsembleCombo.getSelectedItem();
		Ensemble e2 = (Ensemble)this.deuxiemeEnsembleCombo.getSelectedItem();
		Ensemble e3 = e1.intersection(e2);
		
		this.getMere().getMesEnsemble().add(e3);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==this.bouttonConfirmer)
		{
			this.ajouteEnsemble();
		}
	}
}
